﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace MyTools
{
    public class GoogleMapServices
    {
        string MapUrl = "https://maps.google.com/maps/api/geocode/json?region=CAN&language=Canada&sensor=false&key=AIzaSyAUstUuor-kIVq4YeKw89qdpbW-Ee1pS90";


        /// 

        /// 传入经纬度,取得住址
        /// 
        /// 纬度
        /// 经度
        /// 住址
        public string GetAddressByLatLng(string Lat, string Lng)
        {
            string strResult = "";
            strResult=ConvertLatLngToAddress(Lat, Lng);
           
            return strResult;
        }
        /// 

        /// 以住址查询,回传经纬度
        /// 
        /// 
        /// 
        public string  GetLatLngByAddr(string addr)
        {
 
          string _result=  ConvertAddressToLatLng(addr);
            
            
            return _result;
        }
        /// 

        /// 以经纬度调用Google Maps Api,回传JsonResult 
        /// 
        /// 纬度
        /// 经度
        /// 经纬度
        public  string  ConvertLatLngToAddress(string Lat, string Lng)
        {
            string result = string.Empty;

            //string googlemapkey = "AIzaSyBGLwRnlbH6f3NYeXJFlxPT-FUdrHpv_O4";
            string url = MapUrl + "&latlng={0},{1}";
            url = string.Format(url, Lat, Lng);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            using (var response = request.GetResponse())
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {

                result = sr.ReadToEnd();
            }
            return  result ;
        }

        /// 

        /// 以住址去取得Google Maps API Json results
        /// 
        /// 
        /// 
        public string  ConvertAddressToLatLng(string addr)
        {
            string result = string.Empty;

            //string googlemapkey = "AIzaSyBGLwRnlbH6f3NYeXJFlxPT-FUdrHpv_O4";
            string url = MapUrl + "&address={0}";
            url = string.Format(url, addr);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            using (var response = request.GetResponse())
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {

                result = sr.ReadToEnd();
            }

            return  result;
        }
        public class GoogleGeoCodeResponse
        {

            public string status { get; set; }
            public results[] results { get; set; }
        }
        public class results
        {
            public string formatted_address { get; set; }
            public geometry geometry { get; set; }
            public string[] types { get; set; }
            public address_component[] address_components { get; set; }
        }

        public class geometry
        {
            public string location_type { get; set; }
            public location location { get; set; }
        }

        public class location
        {
            public string lat { get; set; }
            public string lng { get; set; }
        }

        public class address_component
        {
            public string long_name { get; set; }
            public string short_name { get; set; }
            public string[] types { get; set; }
        }
    }
}
